import { _decorator, Component, Node } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { UIExitAnimtions } from '../../Init/Tools/UIExitAnimations';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import EventManager from '../../Init/Managers/EventManager';
const { ccclass, property } = _decorator;
/** 补给界面 */
@ccclass('SupplyUI')
export class SupplyUI extends BasicUI {
    @property(Node)
    bg: Node = null;
    @property(Node)
    goldPanel: Node = null;
    @property(Node)
    energyPanel: Node = null;

    curPanel: Node = null;
    isGetAward = false;

    cfg = {
        gold: { award: 300, stepNum: 3 },
        energy: { award: [70, 100], stepNum: 2 },
    }
    isAutoHide = false;

    protected onLoad(): void {
        super.onLoad();
        this.bg.on(Node.EventType.TOUCH_START, this.onCloseClick, this);
    }

    public show(d: { isGoldPanel: boolean, isAutoHide?: boolean }): void {
        super.show();

        this.goldPanel.active = false;
        this.energyPanel.active = false;
        this.isGetAward = false;

        let isGoldPanel = true;
        this.isAutoHide = false;

        if (d) {
            isGoldPanel = d.isGoldPanel;
            if (d.isAutoHide !== undefined) {
                this.isAutoHide = d.isAutoHide;
            }
        }

        if (isGoldPanel) {
            this.curPanel = this.goldPanel;
            this.showGoldPanel();
        } else {
            this.curPanel = this.energyPanel;
            this.showEnergyPanel();
        }
        EventManager.emit(EventTypes.CurLevelEvents.PauseLv);

    }
    public hide(d?: any): void {
        super.hide();
        EventManager.emit(EventTypes.CurLevelEvents.ResumeLv);

    }

    showHideAnim() {
        this.bg.getComponent(UIExitAnimtions).showHideAnim(false);
        this.curPanel.getComponent(UIExitAnimtions).showHideAnim(true, () => {
            //
            UISystem.hideUI(UIEnum.SupplyUI);
        }, true);
    }

    onCloseClick() {
        this.showHideAnim();
    }

    //#region ---金币
    @property(Node)
    goldStepLayer: Node = null;

    showGoldPanel() {
        this.goldPanel.active = true;
        // this.setGoldStepInfo();
        // 视频按钮界面展示LOG
        EventManager.emit(EventTypes.SDKEvents.ShowAdButton, GlobalEnum.VideoIdx.AddGold);
    }

    setGoldStepInfo() {
        const rec = StorageSystem.getData().userAssets.supplyRecord;
        let n = Math.min(rec.gold, this.cfg.gold.stepNum);
        this.goldStepLayer.children[0]
        for (let i = 1; i < this.goldStepLayer.children.length; i++) {
            const e = this.goldStepLayer.children[i];
            e.active = i <= n;
        }
    }
    // 金币视频
    onGoldVideoClick() {
        this.emit(EventTypes.SDKEvents.ShowVideo, () => {
            const userAssets = StorageSystem.getData().userAssets;
            // const rec = userAssets.supplyRecord;
            userAssets.asset += this.cfg.gold.award;
            // 次数+1
            // rec.gold++;
            // this.setGoldStepInfo();
            // if (rec.gold >= this.cfg.gold.stepNum) {
            //     rec.gold = 0;
            // }
            StorageSystem.saveData();
            StorageSystem.updateToAssets(true);
            this.isGetAward = true;

            if (this.isAutoHide) {
                this.showHideAnim();
            }
        }, GlobalEnum.VideoIdx.AddGold);
    }

    onHideGoldPanel() {

    }
    //#endregion

    //#region ---体力
    @property(Node)
    enegryStepLayer: Node = null;

    showEnergyPanel() {
        this.energyPanel.active = true;
        this.setEngeryStepInfo();
        // 视频按钮界面展示LOG
        EventManager.emit(EventTypes.SDKEvents.ShowAdButton, GlobalEnum.VideoIdx.AddEnergy);
    }
    setEngeryStepInfo() {
        const rec = StorageSystem.getData().userAssets.supplyRecord;
        let n = Math.min(rec.energy, this.cfg.energy.stepNum);
        this.enegryStepLayer.children[0].active = true;
        for (let i = 1; i < this.enegryStepLayer.children.length; i++) {
            const e = this.enegryStepLayer.children[i];
            e.active = i <= n;
        }

    }
    onEngeryVideoClick() {
        this.emit(EventTypes.SDKEvents.ShowVideo, () => {
            const userAssets = StorageSystem.getData().userAssets;
            const rec = userAssets.supplyRecord;
            userAssets.energy += this.cfg.energy.award[rec.energy];
            console.log('# + energy', this.cfg.energy.award[rec.energy]);
            // 次数+1
            rec.energy++;
            this.setEngeryStepInfo();
            if (rec.energy >= this.cfg.energy.stepNum) {
                rec.energy = 0;
            }
            StorageSystem.saveData();
            StorageSystem.updateToAssets(true);
            this.isGetAward = true;

            if (this.isAutoHide) {
                this.showHideAnim();
            }
        }, GlobalEnum.VideoIdx.AddEnergy);
    }
    onHideEnergyPanel() {

    }
    //#endregion
}

